package com.kuzlak.cms.security;



import cn.hutool.core.bean.BeanUtil;
import com.kuzlak.cms.module.sys.bean.AccountsVO;
import com.kuzlak.cms.module.sys.entity.SysAccounts;
import com.kuzlak.cms.module.sys.entity.SysUsers;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 封装用户信息
 */
@Data
public class LoginUserDetails implements UserDetails {

    private SysAccounts accounts;
    private AccountsVO accountsVO;

    private List<String> roleNames;

    private List<String> permissionNames;

    public LoginUserDetails(SysAccounts user, List<String> roleNames, List<String> permissionNames) {
        this.accounts = user;
        this.roleNames = roleNames;
        this.permissionNames = permissionNames;
    }
    public LoginUserDetails(SysAccounts accounts, SysUsers user, List<String> roleNames, List<String> permissionNames) {
        AccountsVO   accountsVO = BeanUtil.copyProperties(accounts, AccountsVO.class);
        if(user!=null) {
            accountsVO.setName(user.getName());
            accountsVO.setPhone(user.getPhone());
            accountsVO.setNameOther(user.getNameOther());
            accountsVO.setIdcard(user.getIdcard());
            accountsVO.setWechat(user.getWechat());
        }
        accountsVO.setPermissionNames(permissionNames);
        accountsVO.setRoleNames(roleNames);
        this.accountsVO=accountsVO;

        this.accounts = accounts;
        this.roleNames = roleNames;
        this.permissionNames = permissionNames;
    }

    /**
     * 当前账户的权限列表
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        /*
         * 当前用户的权限信息
         * 1. 角色表权限   ROLE_  admin  ROLE_admin
         * 2. 权限表权限  del  add query edit
         */
        if(!CollectionUtils.isEmpty(roleNames)){
            for (String roleName : roleNames) {
                roleName = "ROLE_"+roleName;
                grantedAuthorities.add(new SimpleGrantedAuthority(roleName));
            }
        }
        if(!CollectionUtils.isEmpty(permissionNames)){
            for (String permissionName : permissionNames) {
                grantedAuthorities.add(new SimpleGrantedAuthority(permissionName));
            }
        }
        return grantedAuthorities;
    }

    /**
     * 获取密码
     */
    @Override
    public String getPassword() {
        return accounts.getUserPassword();
    }

    /**
     * 用户名
     */
    @Override
    public String getUsername() {
        return accounts.getPhone();
    }
    //账户是否过期
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    //账户是否被锁定
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }
    //密码是否过期
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }
    //账户是否有效
    @Override
    public boolean isEnabled() {
        return true;
    }
}
